package com.mogu.biguser.service.impl;

import com.github.pagehelper.PageInfo;
import com.mogu.biguser.common.util.DateUtil;
import com.mogu.biguser.dao.biguser.*;
import com.mogu.biguser.domain.biguser.*;
import com.mogu.biguser.service.StatisticsService;
import com.mogu.biguser.service.dto.biguser.BigUser;
import com.mogu.biguser.service.dto.statistics.ParamTime;
import com.mogu.biguser.service.dto.statistics.StatisticsName;
import com.mogu.biguser.service.dto.statistics.Water;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StatisticsServiceImpl implements StatisticsService {
    private static final Logger logger = LoggerFactory.getLogger(StatisticsServiceImpl.class);

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserCostMapper userCostMapper;

    @Autowired
    private UserWaterMapper userWaterMapper;


    @Autowired
    private UserWaterdayMapper userWaterdayMapper;

    @Autowired
    private UserWatermonthMapper userWatermonthMapper;

    @Autowired
    private UserWateryearMapper userWateryearMapper;


    @Override
    public PageInfo findListStatisName(BigUser user) {
        List<StatisticsName> list = new ArrayList<>();
        PageInfo pageInfo =null;
        try {
         if (user!=null){
             String avgMonth = user.getAvgMonth();
             if (avgMonth!=null && !"".equals(avgMonth)){
                 String[] split = avgMonth.split("-");

                 user.setAvgMonthStart(Integer.valueOf(split[0]));
                 user.setAvgMonthEnd(Integer.valueOf(split[1]));
             }
         }
         list      = userInfoMapper.selListByStatisName(user);
          for(StatisticsName sn:list){
              String userNum = sn.getUserNum();
             List<UserCost> listCost=userCostMapper.selCostByNum(userNum);
              pageInfo= new PageInfo(list);
             if (listCost!=null && listCost.size()!=0){
                 sn.setCostNow("1");}
                 else {

                 sn.setCostNow("0");
             }
          }

            pageInfo.setList(list);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);

        }
        return pageInfo;
    }

    @Override
    public Map<String, Object> findListUserWaterday(ParamTime paramTime) {
        Map<String, Object> map =new HashMap<>();

        List<UserWaterday> supplyDay = new ArrayList<>();
        List<UserWaterday> drainDay = new ArrayList<>();
        Water swater = new Water();
        Water dwater = new Water();
        try{
            paramTime.setTimeEnd(DateUtil.NextDay(paramTime.getTimeEnd()));//推迟一天
            supplyDay =    userWaterdayMapper.selSupplyDayByUserNum(paramTime);
            drainDay =    userWaterdayMapper.selDrainDayByUserNum(paramTime);
            map.put("supplyDay",supplyDay);
            map.put("drainDay",drainDay);
            List<Water> water1 =userWaterdayMapper.selWaterSMax(paramTime);
            if (water1!=null&& water1.size()!=0) {
                swater.setMaxValue(water1.get(0).getMaxValue());
                swater.setAvgValue(water1.get(0).getAvgValue());
                swater.setMaxTime(water1.get(0).getMaxTime());
            }
            water1 =userWaterdayMapper.selWaterSMin(paramTime);
            if (water1!=null&& water1.size()!=0) {
                swater.setMinValue(water1.get(0).getMinValue());
                swater.setMinTime(water1.get(0).getMinTime());
            }
            List<Water> water2 =userWaterdayMapper.selWaterDMax(paramTime);
            if (water2!=null&& water2.size()!=0) {
                dwater.setMaxValue(water2.get(0).getMaxValue());
                dwater.setAvgValue(water2.get(0).getAvgValue());
                dwater.setMaxTime(water2.get(0).getMaxTime());
            }
            water2 =userWaterdayMapper.selWaterDMin(paramTime);
            if (water2!=null&& water2.size()!=0) {
            dwater.setMinValue(water2.get(0).getMinValue());
            dwater.setMinTime(water2.get(0).getMinTime());
            }
            map.put("dwater", dwater);
            map.put("swater", swater);

        } catch (Exception e) {
                e.printStackTrace();
                logger.error(e.getMessage(), e);
            }
        return  map;
    }

    @Override
    public Map<String, Object> findListUserWaterMonth(ParamTime paramTime) {
        Map<String, Object> map =new HashMap<>();

        List<UserWatermonth> supplyMonth = new ArrayList<>();
        List<UserWatermonth> drainMonth = new ArrayList<>();
        Water swater = new Water();
        Water dwater = new Water();
        try{
            paramTime.setTimeEnd(DateUtil.NextMonth(paramTime.getTimeEnd()));//推迟一个月
            supplyMonth =    userWatermonthMapper.selSupplyMonthByUserNum(paramTime);
            drainMonth =     userWatermonthMapper.selDrainMonthByUserNum(paramTime);
            map.put("supplyMonth",supplyMonth);
            map.put("drainMonth",drainMonth);
            List<Water>  water1 =userWatermonthMapper.selWaterSMax(paramTime);
            if (water1!=null&& water1.size()!=0) {
                swater.setMaxValue(water1.get(0).getMaxValue());
                swater.setAvgValue(water1.get(0).getAvgValue());
                swater.setMaxTime(water1.get(0).getMaxTime());
            }
            water1 =userWatermonthMapper.selWaterSMin(paramTime);
            if (water1!=null&& water1.size()!=0) {
                swater.setMinValue(water1.get(0).getMinValue());
                swater.setMinTime(water1.get(0).getMinTime());
            }
            List<Water>  water2 =userWatermonthMapper.selWaterDMax(paramTime);
            if (water2!=null&& water2.size()!=0) {
                dwater.setMaxValue(water2.get(0).getMaxValue());
                dwater.setAvgValue(water2.get(0).getAvgValue());
                dwater.setMaxTime(water2.get(0).getMaxTime());
            }
            water2 =userWatermonthMapper.selWaterDMin(paramTime);
            if (water2!=null&& water2.size()!=0) {
            dwater.setMinValue(water2.get(0).getMinValue());
            dwater.setMinTime(water2.get(0).getMinTime());
            }
            map.put("dwater", dwater);
            map.put("swater", swater);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        return  map;
    }

    @Override
    public Map<String, Object> findListUserWaterYear(ParamTime paramTime) {
        Map<String, Object> map =new HashMap<>();

        List<UserWateryear> supplyYear = new ArrayList<>();
        List<UserWateryear> drainYear = new ArrayList<>();
        Water swater = new Water();
        Water dwater = new Water();
        try{
            paramTime.setTimeEnd(DateUtil.NextYear(paramTime.getTimeEnd()));//推迟一个年
            supplyYear =    userWateryearMapper.selSupplyYearByUserNum(paramTime);
            drainYear =     userWateryearMapper.selDrainYearByUserNum(paramTime);
            map.put("supplyYear",supplyYear);
            map.put("drainYear",drainYear);
            List<Water> water1 =userWateryearMapper.selWaterSMax(paramTime);
            if (water1!=null&& water1.size()!=0) {
                swater.setMaxValue(water1.get(0).getMaxValue());
                swater.setAvgValue(water1.get(0).getAvgValue());
                swater.setMaxTime(water1.get(0).getMaxTime());
            }
            water1 =userWateryearMapper.selWaterSMin(paramTime);
            if (water1!=null&& water1.size()!=0) {
                swater.setMinValue(water1.get(0).getMinValue());
                swater.setMinTime(water1.get(0).getMinTime());
            }
            List<Water> water2 =userWateryearMapper.selWaterDMax(paramTime);
            if (water2!=null&& water2.size()!=0) {
                dwater.setMaxValue(water2.get(0).getMaxValue());
                dwater.setAvgValue(water2.get(0).getAvgValue());
                dwater.setMaxTime(water2.get(0).getMaxTime());
            }
            water2 =userWateryearMapper.selWaterDMin(paramTime);
            if (water2!=null&& water2.size()!=0) {
                dwater.setMinValue(water2.get(0).getMinValue());
                dwater.setMinTime(water2.get(0).getMinTime());
            }
            map.put("dwater", dwater);
            map.put("swater", swater);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        return  map;
    }

    @Override
    public Map<String, Object> findListUserWaterInstant(ParamTime paramTime) {
        Map<String, Object> map =new HashMap<>();

        List<UserWater> list = new ArrayList<>();
        try{
            list=userWaterMapper.selListByUserNum(paramTime);
            map.put("list",list);

           } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
            return map;
        }

    @Override
    public Map<String, Object> findListUserWaterDInstant(ParamTime paramTime) {
        Map<String, Object> map =new HashMap<>();

        List<UserWater> list = new ArrayList<>();
        try{
            list=userWaterMapper.selListDByUserNum(paramTime);
            map.put("list",list);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        return map;
    }

}
